package xyz.xtt.exchange.aop;

import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.adapter.AdvisorAdapter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GreetingAdvice implements MethodBeforeAdvice, AfterReturningAdvice, AdvisorAdapter, MethodInterceptor {
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		// 输出切点
		log.info("before Pointcut:" + target.getClass().getName() + "." + method.getName());
	}

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		log.info("afterReturning Pointcut:" + returnValue);
	}

	@Override
	public boolean supportsAdvice(Advice advice) {
		return true;
	}

	@Override
	public MethodInterceptor getInterceptor(Advisor advisor) {
		return null;
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		try {
			log.info("调用前...");
			return invocation.proceed();
		} catch (Exception e) {
			log.error("error", e);
			throw e;
		} finally {
			log.info("调用后...");
		}
	}
}